import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import toNative from '@/utils/toNative'
import renderModal, {
    getModelValue,
    initModelValue,
    type FormModalDto
} from '@/components/modal/Modal'
import { addInsureIndustry, editInsureIndustry } from '@/api/insureIndustry'
import type { AddType } from '@/api/types'
import type { InsureIndustryListDto } from '@/api/insureIndustry/types'
import { DailyProductTypeCnEnum, ProductTypeCnEnum } from '@/const/enum'
import { message } from 'ant-design-vue'
import type { LabelInValueType } from 'ant-design-vue/es/vc-select/Select'
import { updateIndustry } from '@/api/insurance/daily'
@Component
export class DialogUpdateIndustry extends Render {
    @Prop({ default: false }) show!: boolean
    @Prop() serveCompanyId!: number
    @Prop() productType!: DailyProductTypeCnEnum

    @Emit('update:show')
    showChange(val: boolean) {
        return val
    }

    @Emit('refresh')
    handleRefresh() {}

    @Watch('show', { immediate: true })
    async handleWatchShow() {
        this.formConfig.show = this.show
    }

    formConfig = this.initFormConfig() as FormModalDto

    initFormConfig() {
        return {
            title: '修改行业',
            width: '450px',
            theme: 'dark',
            show: false,
            params: {
                confirmText: '确定'
            },
            items: {
                industry: {
                    name: '行业',
                    element: 'select',
                    optionsType: 'dailyIndustryList',
                    multiple: true,
                    defaultValue: [],
                    params: {
                        maxTagCount: 2,
                        labelInValue: true,
                        optionsParams: {
                            productType: DailyProductTypeCnEnum.日日保,
                            isDelete: 0
                        }
                    }
                }
            },
            complete: async () => {
                const data = getModelValue(this.formConfig)
                if (!data.industry?.length) {
                    message.error('请选择行业')
                    return
                }
                const params = {
                    industryIds: JSON.stringify((data.industry as LabelInValueType[]).map(item => item.value)),
                    industryName: (data.industry  as LabelInValueType[]).map(item => item.originLabel).join(','),
                    productType: this.productType,
                    serveCompanyId: this.serveCompanyId
                }
                await updateIndustry(params)
                this.handleRefresh()
                this.handleClose()
            },
            cancel: () => {
                this.handleClose()
            },
            init: () => {
                initModelValue(this.formConfig)
            }
        } as FormModalDto
    }

    handleClose() {
        this.formConfig.show = false
        this.showChange(false)
    }

    render() {
        return <>{renderModal(this.formConfig)}</>
    }
}

export default toNative<
    { serveCompanyId: number, productType: DailyProductTypeCnEnum },
    { refresh: () => void }
        >(DialogUpdateIndustry)
